﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.ProcessManager.Common.Gateway;

namespace PSE.ProcessManager.Server.InteligenceModules
{
    public class ServiceInteligenceModule : BaseInteligenceModule
    {
        public ServiceInteligenceModule()
        {
            this.Executor = new ServiceProcessExecutor();
            //this.OnProcessReinitializing += new EventHandler(ServiceInteligenceModule_OnProcessReinitializing);
        }

        //void ServiceInteligenceModule_OnProcessReinitializing(object sender, EventArgs e)
        //{
        //    //Cria uma para reiniciar o serviço.
        //    //Message message = new Message();
        //    //message.Operation = "Start";
        //    //this.Execute(message);
        //}

        /// <summary> 
        /// Inicia a execução do processo.
        /// </summary>
        /// <param name="message"></param>
        public override void Execute(BaseMessage message)
        {
            if (String.IsNullOrEmpty(message.Operation))
                throw new ArgumentNullException("message.Operation");

            string operation = message.Operation.ToLower();

            base.Execute(message);

            ServiceProcessExecutor executor = (ServiceProcessExecutor)this.Executor;

            switch (operation)
            {
                case "start":
                    executor.Start();
                    break;
                case "stop":
                    executor.Stop();
                    break;
                case "abort":
                    executor.Abort();
                    break;
                default:
                    executor.Gateway.BeginSendMessage(message);
                    break;
                    //throw new InvalidOperationException(string.Format("Operation '{0}' it´s not supported.", message.Operation));
            }
            //}
        }
    }
}
